w = input().strip()
r = int(input().strip())
c = int(input().strip())
a = []
for i in range(r):
    a.append(input().strip())
total = 0

def findNext(step: dict):
    global total
    print(a[step["indexR"]][step["indexC"]])
    steps.append(step.copy())
    if step["indexW"] >= len(w):
        total += 1
        temp = steps.pop()
        return
    x = -2
    y = -1
    while True:
        x += 1
        if x >= 2:
            x = -1
            y += 1
        if x == 0 and y == 0:
            continue
        if step["indexR"]+x < 0 or step["indexR"]+x >= r or step["indexC"]+y < 0 or step["indexC"]+y >= c:
            if x == 1 and y == 1:
                break
            else:
                continue
        if a[step["indexR"]+x][step["indexC"]+y] == w[step["indexW"]]:
            if not (step["direction_R"] == 0 and step["direction_C"] == 0) and step["isTurn"] and not (step["direction_R"] == x and step["direction_C"] == y):
                continue
            elif not (step["direction_R"] == 0 and step["direction_C"] == 0) and not (step["direction_R"] == x and step["direction_C"] == y) and not (abs(step["direction_R"]+x)+abs(step["direction_C"]+y) == 2):
                continue
            else:
                if not (step["direction_R"] == x and step["direction_C"] == y) and not (step["direction_R"] == 0 and step["direction_C"] == 0):
                    step["isTurn"] = True
                step["direction_R"] = x
                step["direction_C"] = y
                step["indexR"] += x
                step["indexC"] += y
                step["indexW"] += 1
                steps[len(steps)-1]["x"] = x
                steps[len(steps)-1]["y"] = y
                findNext(step)
                step = steps[len(steps)-1].copy()
                x = step["x"]
                y = step["y"]
        if x == 1 and y == 1:
            break
    steps.pop()
    return
steps = []
for i in range(r):
    for j in range(c):
        if a[i][j] == w[0]:
            findNext({"indexW": 1, "indexR": i, "indexC": j,"isTurn": False, "direction_R": 0, "direction_C": 0})
print(total)